package cn.ym.org.controller;

import cn.ym.basic.util.AjaxResult;
import cn.ym.basic.util.PageList;
import cn.ym.org.domain.Employee;
import cn.ym.org.query.EmployeeQuery;
import cn.ym.org.service.EmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 接口类：员工
 */
@RestController//json+controller
//@Controller
//@ResponseBody
@RequestMapping("/employee")
@Api(value = "员工接口类" ,description = "处理员工的所有请求")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    //写一个接口/方法：1.请求方式 2.请求参数 3.请求地址 4.返回数据格式
    /**
     * 接口：查询所有对象
     * @return
     */
    @GetMapping //Get http://localhost:8080/employee
    @ApiOperation(value = "查询所有对象")
    public List<Employee> findAll(){

        return  employeeService.findAll();
    }
    /**
     * 接口：查询单个对象
     * @param id
     * @return
     */
    @GetMapping("/{id}") //Get http://localhost:8080/employee/1
    @ApiOperation(value = "查询单个对象")
    public Employee findOne(@PathVariable("id") Long id){
        return employeeService.findOne(id);
    }

    /**
     * 接口：分页查询或高级查询
     * @param
     * @return
     */
    @PostMapping//Post http://localhost:8080/employee  对象
    @ApiOperation(value = "分页查询或高级查询")
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery employeeQuery){
    return employeeService.queryPage(employeeQuery);
    }

    /**
     * 接口：删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")//Delete http://localhost:8080/employee/1
    @ApiOperation(value = "删除")
    public AjaxResult delete(@PathVariable("id")Long id ){
        try {
            employeeService.delById(id);
            return  new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return  new AjaxResult(false,"删除失败");
        }
    }

    /**
     * 接口：添加或修改
     * @param employee
     * @return
     */
    @PutMapping//Put http://localhost:8080/employee/{对象}
    @ApiOperation(value = "添加或修改")
    public AjaxResult addOrUpdate(@RequestBody Employee employee ){
        try {
            if (employee.getId() == null){//没有id，执行新增
                employeeService.add(employee);
            }else {//有id，执行修改
                employeeService.updateById(employee);
            }
            //返回无参
            return  new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            //返回有参
            return  new AjaxResult(false,"操作失败");
        }
    }

    /**
     * 接口：批量删除
     * @param ids
     * @return
     */
    @PatchMapping //Patch http://localhost:8080/employee/ids
    @ApiOperation(value = "批量删除")
    public AjaxResult patchDelete(@RequestBody List<Long> ids ){
        try {
            employeeService.patchDelete(ids);
            return  new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return  new AjaxResult(false,"批量删除失败");
        }
    }
    /**
     * 接口：员工树
     * @return
     */
/*    @GetMapping("/deptTree") //Get http://localhost:8080/employee/deptTree
    @ApiOperation(value = "员工树")
    public List<Employee> deptTree(){

        return  employeeService.deptTree();
    }*/

}
